package com.hcqc.demo.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.hcqc.demo.pojo.Form1;
import com.hcqc.demo.pojo.Form2;
import com.hcqc.demo.server.Form1Server;
import com.hcqc.demo.server.Form2Server;
import com.hcqc.demo.vo.CompressUtils;
import com.hcqc.demo.vo.DateUtil;
import com.hcqc.demo.vo.ExcelUtil;
import com.hcqc.demo.vo.MultipartFileToFile;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Excel")
public class ExcelController {

    @Autowired
    Form1Server form1Server;
    @Autowired
    Form2Server form2Server;

    //单Excel单sheet导出 Form2所选内容导出
    @RequestMapping("/form2exportExcel")
    public void export2(HttpServletResponse response,HttpServletRequest request,String IDs){
        HttpSession session=request.getSession();
        String user_name = (String) session.getAttribute("USER_NAME");
        List<Form2> list = form2Server.SelectIDs(user_name,IDs);
        System.out.println(list);
        //导出操作
        ExcelUtil.exportExcel(list,"Form2","form2",Form2.class,"Form2.xls",response);
        System.out.println("bbb"+list);
    }
    //单Excel单sheet导出 Form1所选内容导出
    @RequestMapping("/form1exportExcel")
    public void export1(HttpServletResponse response,HttpServletRequest request,String IDs){
        HttpSession session=request.getSession();
        String user_name = (String) session.getAttribute("USER_NAME");
        List<Form1> data = form1Server.SelectIDs(user_name,IDs);
        System.out.println(data);
        //导出操作
        ExcelUtil.exportExcel(data,"Form1","form1",Form1.class,"Form1.xls",response);
    }

    //单Excel多sheet导出
    @RequestMapping("/exportExcel2")
    public void export2(HttpServletResponse response,HttpServletRequest request){

        HttpSession session=request.getSession();
        String user_name = (String) session.getAttribute("USER_NAME");
        //获取数据
        List<Form1> data1 = form1Server.SelectALL(user_name, null);
        List<Form2> data2 = form2Server.SelectALL(user_name);
        System.out.println(data1);
        System.out.println(data2);
        //1把要导出的信息放在map里面
        Map<String, Object> map1 = new HashMap<String, Object>();
        ExportParams exportParams1 = new ExportParams();
        exportParams1.setSheetName("from1");
        exportParams1.setTitle("form1");
        map1.put("title", exportParams1);
        map1.put("entity", Form1.class);
        map1.put("data", data1);
        //2
        Map<String, Object> map2 = new HashMap<String, Object>();
        ExportParams exportParams2 = new ExportParams();
        exportParams2.setSheetName("from2");
        exportParams2.setTitle("form2");
        map2.put("title", exportParams2);
        map2.put("entity", Form2.class);
        map2.put("data", data2);

        List<Map<String, Object>> list=new ArrayList<>();
        list.add(map1);
        list.add(map2);
        //导出操作
        ExcelUtil.exportExcel(list, "ok.xls", response);
    }

    //Excel导入数据库 固定路径 单Excel单sheet
//    @RequestMapping("importExcel")
//    public String importExcel(HttpServletRequest request){
//        HttpSession session=request.getSession();
//        String user_name = (String) session.getAttribute("USER_NAME");
//        String filePath = "D:\\test.xls";
//        //解析excel，
//        List<Form1> form1List = ExcelUtil.importExcel(filePath,1,1,Form1.class);
//        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
//        System.out.println("导入数据一共【"+form1List.size()+"】行");
//
//        form1Server.addFrom1Info(form1List);
//        List<Form1> list = form1Server.SelectALL(user_name, null);
//        return list.toString();
//
//    }


    //文件上传判断
    @RequestMapping("importExcel")
    public void importExcel(@RequestParam("abc") MultipartFile file,HttpServletRequest request) throws IOException {
        //判断Excel中sheet的数量
        Workbook workBook = ExcelUtil.getWorkBook(file);
        int num=workBook.getNumberOfSheets();
        System.out.println(num);
        switch (num){
            case 1:importExcel2(file, request);break;
            case 2:importExcel3(file, request);break;
        }
    }

    //文件上传 Excel导入数据库 单Excel单sheet
    @RequestMapping("importExcel2")
    public List importExcel2(@RequestParam("abc") MultipartFile file,HttpServletRequest request) throws IOException {
        HttpSession session=request.getSession();
        String user_name = (String) session.getAttribute("USER_NAME");
        System.out.println(file.getName());
        //获取sheet名
        Workbook workBook = ExcelUtil.getWorkBook(file);
        String sheetName = workBook.getSheetName(0);
        //解析excel，
        List list = ExcelUtil.importExcel(file,1,1,ExcelUtil.myMap.get(sheetName));
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        System.out.println(list);
        switch (sheetName){
            case "form1":form1Server.addFrom1Info(list,user_name);break;
            case "form2":form2Server.addFrom2Info(list,user_name);break;
            default:return null;
        }

        //文件保存到磁盘
        String sj = DateUtil.GetNow2();
        String filePath="D:\\HC\\Idea\\cadiExcelImpExport\\File\\"+user_name+"\\"+sj+"\\";//文件夹路径
        String fileName = file.getOriginalFilename();//获取文件名
//        System.out.println("上传的文件名为：" + fileName);
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            e.getMessage();
        }

        return list;
    }

    //文件上传 Excel导入数据库 单Excel单多sheet  指定实体
    @RequestMapping("importExcel3")
    public List importExcel3(@RequestParam("abc") MultipartFile file,HttpServletRequest request){
        HttpSession session = request.getSession();
        String user_name = (String) session.getAttribute("USER_NAME");

        System.out.println(file.getName());
        //解析excel，
        List<Form1> form1List = ExcelUtil.importExcel(file,1,1,0,Form1.class);
        List<Form2> form2List = ExcelUtil.importExcel(file,1,1,1,Form2.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("1导入数据一共【"+form1List.size()+"】行");
        System.out.println("2导入数据一共【"+form2List.size()+"】行");
        System.out.println(form1List);
        System.out.println(form2List);
        //添加导入人和导入时间
        for (Form1 form:form1List){
            form.setDRR((String) user_name).setDRSJ(DateUtil.GetNow());
        }
        for (Form2 form:form2List){
            form.setDRR((String) user_name).setDRSJ(DateUtil.GetNow());
        }
        //添加到数据库
        form1Server.addFrom1Info(form1List);
        form2Server.addFrom2Info(form2List);
        List<Form1> list = form1Server.SelectALL(user_name, null);
        List<Form2> list2 = form2Server.SelectALL(user_name);
        System.out.println(list);
        System.out.println(list2);

        //文件保存到磁盘
        String sj = DateUtil.GetNow2();
        String filePath="D:\\HC\\Idea\\cadiExcelImpExport\\File\\"+user_name+"\\"+sj+"\\";//文件夹路径
        String fileName = file.getOriginalFilename();//获取文件名
//        System.out.println("上传的文件名为：" + fileName);
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            e.getMessage();
        }
        return list;
    }

    //压缩包上传
    @RequestMapping("ExcelupZip")
    public void ExcelupZip(@RequestParam("abc") MultipartFile multipartFile,HttpServletRequest request) throws Exception {
        //MultipartFile 转 File
        File zipfile = MultipartFileToFile.multipartFileToFile(multipartFile);
        //解压并保存
        HttpSession session = request.getSession();
        String user_name = (String) session.getAttribute("USER_NAME");
        String sj = DateUtil.GetNow2();
        String dest="D:\\HC\\Idea\\cadiExcelImpExport\\File\\"+user_name+"\\"+sj;//文件夹路径
        System.out.println(dest);
        CompressUtils.unZip(zipfile, dest, null);
        //读取文件
        File file = new File(dest);
        File[] tempList = file.listFiles();
        //导入数据
        for (File f:tempList){
            //File转MultipartFile
            MultipartFile mFile = MultipartFileToFile.FileTomultipartFile(f);
            //文件判断
            importExcel(mFile,request);
        }
    }

}
